package com.hs.web;

import com.hs.pojo.User;
import com.hs.service.UserService;
import com.hs.service.impl.UserServiceImpl;
import com.hs.util.Page;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/user/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1、获取请求中的action
        String action =req.getParameter("action");
        //根据action 不同的参数去处理不同的功能
        //selectUsers: 分页操作  insertUsers添加 updateUsers deleteUsers
        if("selectUsers".equals(action)){
            //动态获取客户想要获取的页码
            String pn=req.getParameter("pageNum");
            if(pn ==null){
                pn = "1";
            }
            int pageNum=Integer.parseInt(pn);

            UserService userService=new UserServiceImpl();
            Page<User> page = userService.selectUsers(pageNum);
            //封装page对象，获取分页数据
            req.setAttribute("page",page);
            req.getRequestDispatcher("/pages/user-list.jsp").forward(req,resp);
        }else if("insertUsers".equals(action)){
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            User user=new User(0,username,password,email,phone);
            UserService us=new UserServiceImpl();
            int add = us.insertUser(user);
            //给出响应
            String mess="";
            if(add != 0){
                mess="添加成功";
            }else{
                mess="添加失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/user/userServlet?action=selectUsers").forward(req,resp);

        }else if("selectUserById".equals(action)){
            String id=req.getParameter("id");
            UserService us=new UserServiceImpl();
            User user = us.SelectUserById(Integer.parseInt(id));
            req.setAttribute("user",user);
            req.getRequestDispatcher("/pages/user-update.jsp").forward(req,resp);
        }else if ("updateUsers".equals(action)){
            String id=req.getParameter("id");
            String username = req.getParameter("username");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");

            User user=new User(Integer.parseInt(id),username,"",email,phone);
            UserService us=new UserServiceImpl();
            int update = us.updateUser(user);
            //给出响应
            String mess="";
            if(update != 0){
                mess="修改成功";
            }else{
                mess="修改失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/user/userServlet?action=selectUsers").forward(req,resp);

        }else if ("deleteUsers".equals(action)){
            String id=req.getParameter("id");
            UserService us=new UserServiceImpl();
            int delete = us.deleteUser(Integer.parseInt(id));
            //给出响应
            String mess="";
            if(delete != 0){
                mess="修改成功";
            }else{
                mess="修改失败";
            }
            req.setAttribute("mess",mess);
            req.getRequestDispatcher("/user/userServlet?action=selectUsers").forward(req,resp);

        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
